﻿/*
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <unistd.h>
#include <pthread.h>

#include "poolsocket.h"

/*
typedef struct _SCKClitPoolParam
{
	char serverip[64];
	int serverport;
	//14.池容量；
	int bounds;
	int connecttime;
	int sendtime;
	int revtime;

}SCKClitPoolParam;
*/

/*
void* mystart_routine(void* arg)
{
	int ret = 0;
	void* handle = arg;
	int connfd = -1;

	unsigned char* data = "abcdefg";
	int datalen = 5;

	unsigned char* out = NULL;
	int outlen = -1;

	//3.获取一条连接池中的连接；
	ret = sckCltPool_getConnect(handle, &connfd);
	if (ret != 0)
	{
		printf("从连接池，获取连接失败：%d\n", ret);
		return NULL;
	}

	while (1)
	{
		ret = sckCltPool_send(handle, connfd, data, datalen);
		if (ret == Sck_ErrPeerClosed)
		{
			//4.检测到，对（方）端关闭，则关闭本端；
			//sckServer_close(connfd);
			//printf("---ErrPeerClosed \n");
			printf("---服务器关闭，客户端断开连接 \n");
			//return NULL;
			break;
		}
		else if (ret == Sck_ErrTimeOut)
		{
			//5.一般在并发访问量较高时，才会出现这样的情况；
			//printf("---服务器检测到本端发送数据-超时 \n");
			printf("----服务器接收数据-超时 \n");
			//return NULL;
			continue;
		}
		else if (ret != 0)
		{
			//printf("未知错误\n");
			printf("客户端发送数据失败：errorNO: %d\n", ret);
			//return NULL;
			break;
		}


		ret = sckCltPool_rev(handle, connfd, &out, &outlen);
		if (ret == Sck_ErrPeerClosed)
		{
			//6.检测到，对（方）端关闭，则关闭本端；
			//sckServer_close(connfd);
			//printf("---ErrPeerClosed \n");
			printf("---服务器关闭，客户端断开连接 \n");
			//return NULL;
			break;
		}
		else if (ret == Sck_ErrTimeOut)
		{
			//7.一般在并发访问量较高时，才会出现这样的情况；
			//printf("---服务器检测到本端发送数据-超时 \n");
			//printf("----服务器接收数据-超时 \n");
			printf("----服务器发送数据-超时 \n");
			//return NULL;
			continue;
		}
		else if (ret != 0)
		{
			//printf("未知错误\n");
			//printf("客户端发送数据失败：errorNO: %d\n", ret);
			printf("客户端接收数据失败：errorNO: %d\n", ret);
			//return NULL;
			break;
		}
		printf("------------接收到，服务器回发数据：%s\n", out);
	}

	sckCltPool_putConnect(handle, connfd, 0);

	return NULL;
}

int main347(void)
{
	/*
	char* ip = "127.0.0.1";
	int port = 8080;
	//int connecttime = 3;
	int time = 3;
	int connfd = -1;

	int ret = -1;

	unsigned char* data = "abcdefg";
	int datalen = 5;

	unsigned char* out = NULL;
	int outlen = -1;
	*/
	/*
		SCKClitPoolParam clientPoolParam;
		strcpy(clientPoolParam.serverip, "127.0.0.1");
		clientPoolParam.serverport = 8080;
		clientPoolParam.bounds = 10;
		clientPoolParam.connecttime = 3;
		clientPoolParam.sendtime = 3;
		clientPoolParam.revtime = 3;

		int ret = 0;
		void* handle = NULL;
		int i = 0;
		pthread_t pidArray[6] = { 0 };
	*/
	/*
		//1.客户端，初始化；
		//ret = sckClient_init();
		ret = sckCltPool_int(&handle, &clientPoolParam);
		if (ret != 0)
		{
			//printf("sckClient_init error %d\n", ret);
			printf("sckCltPool_init error %d\n", ret);
			return ret;
		}

		/*
		while (1)
		{
			//2.客户端，连接服务器；
			//ret = sckClient_connect(ip, port, connecttime, &connfd);
			ret = sckClient_connect(ip, port, time, &connfd);
			if (ret == Sck_ErrTimeOut)
			{
				printf("----客户端连接服务器-超时 \n");
				continue;
			}
			else if (ret != 0)
			{
				printf("客户端连接服务器-失败：errorNO: %d\n", ret);
				break;
			}

			//4.客户端，发送报文；
			ret = sckClient_send(connfd, time, data, datalen);
			if (ret == Sck_ErrPeerClosed)
			{
				//7.检测到，对（方）端关闭，则关闭本端；
				//sckServer_close(connfd);
				//printf("---ErrPeerClosed \n");
				printf("---服务器关闭，客户端断开连接 \n");
				//return NULL;
				break;
			}
			else if (ret == Sck_ErrTimeOut)
			{
				//8.一般在并发访问量较高时，才会出现这样的情况；
				//printf("---服务器检测到本端发送数据-超时 \n");
				printf("----服务器接收数据-超时 \n");
				//return NULL;
				continue;
			}
			else if (ret != 0)
			{
				//printf("未知错误\n");
				printf("客户端发送数据失败：errorNO: %d\n", ret);
				//return NULL;
				break;
			}
			sleep(1);

			//5.客户端，接收报文；1
			ret = sckClient_rev(connfd, time, &out, &outlen);
			if (ret == Sck_ErrPeerClosed)
			{
				//9.检测到，对（方）端关闭，则关闭本端；
				//sckServer_close(connfd);
				//printf("---ErrPeerClosed \n");
				printf("---服务器关闭，客户端断开连接 \n");
				//return NULL;
				break;
			}
			else if (ret == Sck_ErrTimeOut)
			{
				//10.一般在并发访问量较高时，才会出现这样的情况；
				//printf("---服务器检测到本端发送数据-超时 \n");
				//printf("----服务器接收数据-超时 \n");
				printf("----服务器发送数据-超时 \n");
				//return NULL;
				continue;
			}
			else if (ret != 0)
			{
				//printf("未知错误\n");
				//printf("客户端发送数据失败：errorNO: %d\n", ret);
				printf("客户端接收数据失败：errorNO: %d\n", ret);
				//return NULL;
				break;
			}

		}*/

		/*
			while (1)
			{
				for (i = 0; i < 6; i++)
				{
					pthread_create(&pidArray[i], NULL, mystart_routine, handle);
				}

				for (i = 0; i < 6; i++)
				{
					pthread_join(pidArray[i], NULL);
				}
			}


			/*
			if (connfd != -1)
			{
				//3.客户端，关闭和服务端的连接；
				int sckClient_closeconn(int connfd);
			}

			//6.客户端，释放；
			sckClient_destroy();
			*/
			/*
				//2.销毁连接池；
				sckCltPool_destroy(handle);

				return 0;
			}
			*/